<?php

namespace app\common\repositories\housing;

use think\facade\Db;
use app\common\dao\housing\HousingLogDao;
use app\common\repositories\BaseRepository;

/**
 * Class HousingLogRepository
 *
 * @mixin HousingLogDao
 */
class HousingLogRepository extends BaseRepository
{

    public function __construct(HousingLogDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'userInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['username' => 'username']);
                }
            ])
            ->order('id', 'desc')
            ->select();
        return compact('list', 'count');
    }

    /**
     * 添加房间日志
     * @param int $propertyId
     * @param array $data
     * @param int $userId
     * @return void
     *
     * @author Amos
     * 2024-05-10 15:07
     */
    public function addData(int $propertyId, array $data, int $userId)
    {
        $data['user_id'] = $userId ?? '0';
        $data['property_id'] = $propertyId;
        $data['track_ip'] = app('request')->ip();
        $this->dao->create($data);
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        $this->dao->delete($info['id']);
    }

}
